package com.neusoft.javafeatures9.clickhouse;

import com.clickhouse.jdbc.ClickHouseDataSource;
//import sun.security.ssl.SSLContextImpl;
//import sun.invoke.util.*;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class ClickhouseDemo {
    /**
     * DBeaver连接clickhouse（超详细图文教程） https://blog.csdn.net/fy512/article/details/122559936
     * github 官方jdbc demo: https://github.com/ClickHouse/clickhouse-jdbc
     * 官方 docs: ClickHouse Playground https://clickhouse.com/docs/zh/getting-started/playground
     */
    public static void main(String[] args) throws Exception {
        // jdbc:(ch|clickhouse):[defaultProtocol://]endpoint1[,endpoint2,endpoint3,...][/defaultDatabase][?defaultParameters][#efaultTags]
        String url = "jdbc:clickhouse:https://play.clickhouse.com:443/default";
        Properties properties = new Properties();
        properties.setProperty("user", "explorer");
        properties.setProperty("password", "");
        // optional properties
        properties.setProperty("client_name", "Agent #1");

        //模块化测试
//        SSLContextImpl.DefaultSSLContext defaultSSLContext;
//        BytecodeDescriptor descriptor = null;
//        SSLContextImpl.DefaultSSLContext newInstance = null;

        // 一个jar包包含两个驱动
            // driver class name:  ru.yandex.clickhouse.ClickHouseDriver 官方不推荐，历史名称
            // driver class name:  com.clickhouse.jdbc.ClickHouseDriver  官方推荐
            ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties);
        try (Connection conn = dataSource.getConnection();
             Statement stmt = conn.createStatement();
             /**
              * default
              * git_clickhouse
              * mgbench
              */
             ResultSet rs = stmt.executeQuery("show databases")) {
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        }
    }
}
